﻿using Nop.Core;
using Nop.Core.Domain.Orders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nop.Web.Controllers
{
    public class Utilities
    {
        //public static List<ShoppingCartItem> GetCart(IWorkContext _workContext, HttpContextBase _httpContext, IStoreContext _storeContext)
        //{
        //    List<ShoppingCartItem> cart = new List<ShoppingCartItem>();
        //    List<ShoppingCartItem> cartToSession = new List<ShoppingCartItem>();

        //    //validation
        //    cart = _workContext.CurrentCustomer.ShoppingCartItems
        //        .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
        //        .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id)
        //        .ToList();

        //    if (cart.Count == 0)
        //        if (_httpContext.Session["SessionShoppingCartItems"] != null)
        //            cart = (List<ShoppingCartItem>)_httpContext.Session["SessionShoppingCartItems"];


        //    foreach (var item in cart)
        //    {
        //        cartToSession.Add(item);
        //    }
        //    _httpContext.Session["SessionShoppingCartItems"] = cartToSession;

        //    _workContext.CurrentCustomer.ShoppingCartItems = cart;

        //    //_customerService.UpdateCustomer(_workContext.CurrentCustomer);
        //    return cart;
        //}

        public static IWorkContext GetWorkContext(IWorkContext _workContext, HttpSessionStateBase _httpContext, IStoreContext _storeContext)
        {
            IWorkContext NewContext = null;
            List<ShoppingCartItem> cart = new List<ShoppingCartItem>();

            //validation
            cart = _workContext.CurrentCustomer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
                .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id)
                .ToList();

            if (cart.Count == 0)
            {
                if (_httpContext["SessionWorkContext"] != null)
                    NewContext = (IWorkContext)_httpContext["SessionWorkContext"];
            }
            else
                NewContext = _workContext;

            _httpContext["SessionWorkContext"] = NewContext;
            _workContext = NewContext;

            return NewContext;

        }

        public static List<ShoppingCartItem> GetCart(IWorkContext _workContext, HttpSessionStateBase _httpContext, IStoreContext _storeContext)
        {
            List<ShoppingCartItem> cart = new List<ShoppingCartItem>();
            List<ShoppingCartItem> cartToSession = new List<ShoppingCartItem>();

            //validation
            cart = _workContext.CurrentCustomer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
                .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id)
                .ToList();

            if (cart.Count == 0)
                if (_httpContext["SessionShoppingCartItems"] != null)
                    cart = (List<ShoppingCartItem>)_httpContext["SessionShoppingCartItems"];


            foreach (var item in cart)
            {
                cartToSession.Add(item);
            }
            _httpContext["SessionShoppingCartItems"] = cartToSession;

            _workContext.CurrentCustomer.ShoppingCartItems = cart;

            return cart;
        }

    }
}